﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Common
{
    public class StringHelper
    {
        /// <summary>
        /// 过滤脏字符串
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string FilterBadWords(string Msg)
        {
            string badwords = "妈妈的|我靠|操|fuck|sb|bitch|他妈的|性爱|操你妈|三级片|sex|腚|妓|娼|阴蒂|奸|尻|贱|婊|靠|叉|龟头|屄|赑|妣|肏|尻|屌";
            string[] tempstr = badwords.Split('|');
            string finalstr = Msg;
            for (int i = 0; i < tempstr.Length; i++)
            {
                finalstr = finalstr.Replace(tempstr[i], new String('*', tempstr[i].Length));
            }
            return finalstr;

        }
        /// <summary>
        /// 截取指定长度的字符串
        /// </summary>
        /// <param name="Value">字符串</param>
        /// <param name="Length">截取长度</param>
        /// <returns></returns>
        public static string CutString(string Value, int Length)
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            int num = 0;
            string str = "";
            byte[] bytes = encoding.GetBytes(Value);
            for (int i = 0; i < bytes.Length; i++)
            {
                if (bytes[i] == 0x3f)
                    num += 2;
                else
                    num++;
                try
                {
                    str += Value.Substring(i, 1);
                }
                catch
                {
                    return str;
                }
                if (num > Length)
                    return str;
            }
            return str;
        }
        /// <summary>
        /// 随机生产指定位字母加数字组合的字符串(递归)
        /// </summary>
        /// <param name="VcodeNum">位数</param>
        /// <returns>随机字符串</returns>
        public static string RandString(int VcodeNum)
        {
            string[] strArray = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,W,X,Y,Z".Split(new char[] { ',' });
            string str2 = "";
            int num = -1;
            Random random = new Random();
            for (int i = 1; i < (VcodeNum + 1); i++)
            {
                if (num != -1)
                    random = new Random((i * num) * ((int)DateTime.Now.Ticks));
                int index = random.Next(strArray.Length);
                if ((num != -1) && (num == index))
                    return RandString(VcodeNum);
                num = index;
                str2 = str2 + strArray[index];
            }
            return str2;
        }
        /// <summary>
        /// 产生GUID
        /// </summary>
        /// <returns></returns>
        public static string GetGuid()
        {
            Guid guid = Guid.NewGuid();
            return guid.ToString();
        }
        /// <summary>
        /// 以时间为种子生成文件名称
        /// </summary>
        /// <returns></returns>
        public static string GenerateFileName()
        {
            return DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString();

        }
        /// <summary>
        /// 判断字符串中是否存在指定字符串
        /// </summary>
        /// <param name="Str"></param>
        /// <param name="HasStr"></param>
        /// <returns></returns>
        public static bool GetChar(string Str, string HasStr)
        {
            int i = Str.IndexOf(HasStr);
            if (i != -1)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 正则表达式剔除Html标签得到纯净文本.
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <returns></returns>
        public static string ClearHtml(string htmlContent)
        {
            string strOutput = htmlContent;
            Regex regex = new Regex("<[^>]+>|]+>");
            strOutput = regex.Replace(strOutput, "");
            return strOutput;
        }
    }
}